package com.txby.platform.component.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.jck.mybatis.log.GlobalLogAnnotation;
import com.txby.platformCore.data.CommonApiModelMapExt;
import com.txby.platformCore.entity.News;

import io.swagger.annotations.Api;

/**
 * 新闻编辑
* @author lzq
* @date  
 */
@SuppressWarnings("all")
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@Api(value="保存修改单个新闻-管理端",description="保存修改单个新闻-说明")
@RequestMapping("/news")
public class NewsController extends CommonController {
	
	private static Logger log = Logger.getLogger(NewsController.class);

/*	@ApiOperation(value = "保存修改单个新闻", notes = "保存修改单个新闻-说明")
	@PostMapping("/saveNewsInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true)
	})*/
	@PostMapping("/saveNewsInfo")
	@GlobalLogAnnotation(logType=2,discription="保存修改单个新闻")
	public Map saveNewsInfo(/*@PathVariable("type") Integer type, @PathVariable("token") String token,*/ @RequestParam("upfile") MultipartFile file,News news,HttpServletRequest request,HttpServletResponse response) {
		
        response.setContentType("text/html; charset=utf-8"); //以下为允许跨域的一些设置
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("x-frame-options", "ALLOW-FROM *");
        response.addHeader("Content-Security-Policy","frame-ancestors *");
        response.setHeader("Access-Control-Allow-Headers","Access-Control");
        response.setHeader("Access-Control-Allow-Methods","POST,GET");
//        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		
//		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		CommonApiModelMapExt mm = new CommonApiModelMapExt();
		
		
	    String originname=file.getOriginalFilename();
        int doc=originname.lastIndexOf(".");
/*        String suffix=originname.substring(doc); //得到后缀名
        String uuid= UUID.randomUUID().toString(); //使用uuid命名新文件
        String finalname= JxAppFoundation.WikiFile+uuid+suffix; //新文件的存储位置全路径名
        BufferedOutputStream stream = null;
        try {
            stream = new BufferedOutputStream(
                    new FileOutputStream(new File(finalname)));

            FileCopyUtils.copy(file.getInputStream(), stream);
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        long size=file.getSize();
        String ret=String.format("{\"originalName\":\"%s\"," +
                "\"name\":\"%s\"," +
                "\"url\":\"%s\"," +
                "\"size\":\"%s\"," +
                "\"type\":\"%s\"," +
                "\"state\":\"%s\"}",
                originname,"",Long.toString(size),"","SUCCESS"); //返回字符串，返回的url参数为新文件名
		
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (news != null) {
				this.mBaseDao.saveObject(news);
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
}